package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.JobDemandMapper;
import com.example.pojo.JobDemand;
import com.example.service.IJobDemandService;
import com.example.util.IdUtils;
import com.example.vo.ResultVo;
import com.example.vo.per.DemandAppQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位需求计划表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-10
 */
@Service
public class JobDemandServiceImpl extends ServiceImpl<JobDemandMapper, JobDemand> implements IJobDemandService {

    @Autowired
    private JobDemandMapper jdm;

    /**
     * 根据编号查询
     * @param rid
     * @return
     */
    @Override
    public JobDemand selById(String rid) {
        return jdm.selectById(rid);
    }

    @Override
    public List<Map<String, Object>> selEmpByJob() {
        return jdm.selByJob();
    }

    @Override
    public ResultVo selByNumStatus() {
        List<JobDemand> list = jdm.selectPotiname();
        if (!list.isEmpty()){
            return ResultVo.response(list);
        }
        return ResultVo.error("已结束岗位招聘！");
    }

    @Override
    public Integer intjob(JobDemand jrp) {
//        jrp.setId(new IdUtils().getIncreaseIdByNanoTime());
        jrp.setProcessId("1");
        jrp.setCreateTime(LocalDateTime.now());
        jrp.setStatus(0);
        jrp.setDeleted(0);
        return jdm.insert(jrp);
    }

    /**
     * 多条件查询
     * @param daq 条件
     */
    @Override
    public PageInfo<JobDemand> selByMap(DemandAppQuery daq) {
        QueryWrapper<JobDemand> wrapper = new QueryWrapper<>();
        wrapper.eq(daq.getEmp_id()!=null,"emp_id",daq.getEmp_id())
                .eq(daq.getPosition_id() != null,"position_id",daq.getPosition_id())
                .eq(daq.getStatus() != null,"status",daq.getStatus())
                .orderByDesc("create_time");
        PageHelper.startPage(daq.getPagerNum(),daq.getPageSize());
        List<JobDemand> list = jdm.selectList(wrapper);
        return new PageInfo<>(list);
    }

    /**
     * 逻辑删除
     * @param ids 编号集合
     * @return
     */
    @Override
    public Integer uptById(String[] ids) {
        for (String id : ids) {
            JobDemand jd = new JobDemand();
            jd.setId(id);
            jd.setDeleted(1);
            jd.setStatus(3);
            jdm.updateById(jd);
        }
        return 1;
    }

    /**
     * 发布岗位
     * @param rid 状态
     * @param distinguish 区分
     */
    @Override
    public Integer uptByIdStatus(String rid,String distinguish) {
        JobDemand jd = new JobDemand();
        jd.setId(rid);
        if ("发布".equals(distinguish)){
            jd.setStatus(1);
            return jdm.updateById(jd);
        }
       if ("取消".equals(distinguish)){
           jd.setStatus(0);
           return jdm.updateById(jd);
       }
        return 0;
    }
}
